package ru.spies.work;

import javax.servlet.http.HttpServletRequest;

import ru.spies.content.SystemContent;
import ru.spies.forms.EDBTables;
import ru.spies.forms.LastCookies;
import ru.spies.forms.ESystemFunction;

public class ShowResults {
	
	private static ShowResults statThis=null;
	private static EDBTables lastTable=null;
	private static String lastCols[]=null;
	
	private static String mainpath;
	private static final String FILE_NAME="showTable";
	
	private static String TABLE_NAME="someTable";
	
	private static String COLUMNS_AGENT[]={"recruiter","department",
		"name","countRecruits","status"};
	private static String COLUMNS_AGENT_AND_TASK[]={"agent","task"};
	private static String COLUMNS_COUNTRY[]={"name","relation"};
	private static String COLUMNS_DEPARTMENT[]={"country","resident",
		"name","description","creationDate"};
	private static String COLUMNS_DOCUMENT[]={"task","importance",
		"textLink"};
	private static String COLUMNS_IMPORTANCE[]={"type"};
	private static String COLUMNS_TASK_TYPE[]={"typeName"};
	private static String COLUMNS_TASK[]={"importance","country",
		"type","name","status"};
	
	private static String NORMAL_NAME_COLUMNS_AGENT[]={"Вербовщик","Отдел",
		"Имя агента","Завербовал","Раскрыт?"};
	private static String NORMAL_NAME_COLUMNS_AGENT_AND_TASK[]={"Агент","Задание"};
	private static String NORMAL_NAME_COLUMNS_COUNTRY[]={"Наименование","Отношения"};
	private static String NORMAL_NAME_COLUMNS_DEPARTMENT[]={"Страна","Главный",
		"Наименование отдела","Описание","Дата создания"};
	private static String NORMAL_NAME_COLUMNS_DOCUMENT[]={"Задание","Секретность",
		"Ссылка на документ"};
	private static String NORMAL_NAME_COLUMNS_IMPORTANCE[]={"Тип секретности"};
	private static String NORMAL_NAME_COLUMNS_TASK_TYPE[]={"Тип задания"};
	private static String NORMAL_NAME_COLUMNS_TASK[]={"Секретность","Страна",
		"Тип задания","Наименование задания","Статус выполнения"};

	private ShowResults() {
		mainpath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();	
		mainpath=mainpath.replaceFirst("/", "");
		mainpath=mainpath.replaceAll("WEB-INF/classes/", "");
	}
	
	public static ShowResults getInstance(){
		if (statThis==null){
			statThis = new ShowResults();
		}
		return statThis;
	}

	private String createJSFunction(){
		String cols[];
		lastTable=LastCookies.getInstance().getLastTable();
		if (lastTable==null){
			return "";
		}
		switch (lastTable) {
		case AGENT:
			cols = COLUMNS_AGENT;
			lastCols= NORMAL_NAME_COLUMNS_AGENT;
			break;
		case AGENT_AND_TASK:
			cols = COLUMNS_AGENT_AND_TASK;
			lastCols= NORMAL_NAME_COLUMNS_AGENT_AND_TASK;
			break;
		case COUNTRY:
			cols = COLUMNS_COUNTRY;
			lastCols= NORMAL_NAME_COLUMNS_COUNTRY;
			break;
		case DEPARTMENT:
			cols = COLUMNS_DEPARTMENT;
			lastCols= NORMAL_NAME_COLUMNS_DEPARTMENT;
			break;
		case DOCUMENT:
			cols = COLUMNS_DOCUMENT;
			lastCols= NORMAL_NAME_COLUMNS_DOCUMENT;
			break;
		case IMPORTANCE:
			cols = COLUMNS_IMPORTANCE;
			lastCols= NORMAL_NAME_COLUMNS_IMPORTANCE;
			break;
		case TASK:
			cols = COLUMNS_TASK;
			lastCols= NORMAL_NAME_COLUMNS_TASK;
			break;
		case TASK_TYPE:
			cols = COLUMNS_TASK_TYPE;
			lastCols= NORMAL_NAME_COLUMNS_TASK_TYPE;
			break;	
		default:
			return "";
		}
		return SystemContent.getInstance().jsFileForDataTableContent(TABLE_NAME, FILE_NAME, cols);
	}

	public String getJSSource(){
		String scriptBody = createJSFunction();
		if (scriptBody==null){
			return "";
		}
		return "<script>"+scriptBody+"</script>";
	}
	
	public String getTable(){
		return SystemContent.getInstance().getDataTableContent(TABLE_NAME, lastCols);
	}
	
	public boolean chooseWork(HttpServletRequest request){
		ESystemFunction lastFunction=LastCookies.getInstance().getLastSystemCookie();
		lastTable=LastCookies.getInstance().getLastTable();
		if ((lastFunction==null)||(lastTable==null)){
			return false;
		}
		switch (lastFunction) {
		case ADD_NEW_TABLE_OBJECT:
			AddWork.getInstance().doWork(request,lastTable);
			ShowWork.getInstance().doWork(request,lastTable);
			break;
		case DB_FUNCTIONAL:
			return false;
		case DELETE_OBJECT_FROM_TABLE:
			DeleteWork.getInstance().doWork(request,lastTable);
			ShowWork.getInstance().doWork(request,lastTable);
			break;
		case SHOW_OBJECTS:
			ShowWork.getInstance().doWork(request,lastTable);
			break;
		case UPDATE_OBJECT:
			UpdateWork.getInstance().doWork(request,lastTable);
			ShowWork.getInstance().doWork(request,lastTable);
			break;	
		default:
			return false;
		}
		return true;
	}

	public String getFileName() {
		return FILE_NAME;
	}
	
	public String getMainPath(){
		return mainpath;
	}

}
